package com.yc.book.util;

import java.util.Collection;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.yc.book.biz.BizException;

/**
 * 工具类
 */
public class Utils {

	// 检查值是否异常
	public static void check(boolean condition, String msg) throws BizException {
		if (condition) {
			throw new BizException(msg);
		}
	}

	// 判断空对象,空字符串,空集合
	public static boolean isEmpty(Object obj) {
		if (obj == null) {
			// 判断空对象
			return true;
		} else if (obj instanceof String) {
			// 判断空字符串
			return obj.toString().trim().isEmpty();
		} else if (obj instanceof Collection) {
			// 判断集合
			return ((Collection<?>) obj).isEmpty();
		} else if (obj instanceof Map) {
			// 判断空字符串
			return ((Map<?, ?>) obj).isEmpty();
		} else {
			return false;
		}
	}
	
	public static void showMessage(Shell shell, String msg) {
		showMessage(shell, "系统提示", msg);
	}
	
	public static void showMessage(Shell shell, String title, String msg) {
		MessageBox mb = new MessageBox(shell);
		mb.setText(title);
		mb.setMessage(msg);
		mb.open();
	}
	
	/**
	 * 用户确认窗口
	 * @param shell
	 * @param msg
	 * @return
	 */
	public static boolean confirm(Shell shell,  String msg) {
		MessageBox mb = new MessageBox(shell, SWT.OK | SWT.CANCEL);
		mb.setText("请问");
		mb.setMessage(msg);
		int  ret = mb.open();
		return ret == SWT.OK;
	}

}
